package com.iss.soc;

import com.iss.soc.httpclient.RestClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2016/7/14 0014.
 */
public class ApiEnvironment {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    protected Map<String, String> map = new HashMap<String, String>();
    static final String URL = "url";

    private ApiEnvironment(String propertiesPath) {
        Properties props = new Properties();
        try {
            InputStream in = new BufferedInputStream(new FileInputStream(propertiesPath));
            props.load(in);
            if (props.containsKey(URL)) {
                configureSaibong(props);
            }

        } catch (Exception e) {
            logger.error("加载服务配置文件失败");
        }

    }

    //配置信息
    private void configureSaibong(Properties props) {
        if (StringUtils.isNotEmpty(props.getProperty(URL))) {
            String value = props.getProperty(URL);
            map.put(URL, value);
        }

    }


    /**
     * 为设置身份验证提供了Azure。
     */
    private static final ApiEnvironment API = new ApiEnvironment(
            ApiEnvironment.class.getClassLoader().getResource("soc.properties").getPath());

    public static ApiEnvironment getApi() {
        return API;
    }

    /**
     * 构建服务
     *
     * @return a builder for the rest client.
     */
    public RestClient.Builder.Buildable newRestSaibongClientBuilder() {
        return new RestClient.Builder()
                .withDefaultBaseUrl(map.get(URL));
    }

}
